<?php

// 加载Codestar框架
if (!class_exists('CSF')) {
    require_once get_template_directory() . '/inc/codestar-framework/codestar-framework.php';
}

new ZB_Admin();
/**
 * 后台优化 管控 修改登录安全验证等信息
 */
class ZB_Admin {

    private $safe_login_key; //自带登录页参数
    private $safe_login_password; //自带登录页密码
    private $safe_login_user_id; //允许进入后台管理员ID

    public function __construct() {

        $this->safe_login_key      = 'security';
        $this->safe_login_password = _xin('site_login_security_param', '');
        $this->safe_login_user_id  = '';
        
        // 初始化Codestar框架选项

              
        //删除个人资料后台页面无用字段
        add_action('admin_init', array($this, 'remove_profile_fields'));

        // //移除后台页面标题中的“ — WordPress”
        add_filter('admin_title', function ($admin_title, $title) {
            return $title . ' &lsaquo; ' . get_bloginfo('name');
        }, 10, 2);

        add_filter('login_title', function ($login_title, $title) {
            return $title . ' &lsaquo; ' . get_bloginfo('name');
        }, 10, 2);

        add_filter('login_headerurl', function ($url) {
            return home_url();
        });

        add_filter('login_display_language_dropdown', '__return_false');

      

        //上传文件重命名
        add_filter('sanitize_file_name', array($this, 'update_file_md5_rename'), 10);

        // 仅在媒体库查询时应用过滤器
        add_action('ajax_query_attachments_args', array($this, 'restrict_media_library'));
        //默认可视化编辑器
        add_filter('wp_default_editor', function(){
            return 'tinymce';
        });
        
    }
    
    public function restrict_media_library($query) {

        // 仅当用户具有manage_options权限时应用过滤器
        if (current_user_can('manage_options')) {
            return $query;
        }

        // 获取当前登录用户的ID
        $current_user_id = get_current_user_id();

        // 设置查询参数，仅返回当前用户上传的附件
        $query['author'] = $current_user_id;

        return $query;
    }

    public function update_file_md5_rename($filename) {

        if (!_xin('site_update_file_md5_rename',false)) {
            return $filename;
        }

        $info = pathinfo($filename);
        $ext  = empty($info['extension']) ? '' : '.' . $info['extension'];
        $name = basename($filename, $ext);
        return substr(md5($name), 0, 15) . $ext;

    }

    public function load_view($template) {
        $views_dir = get_template_directory() . '/admin/pages/';
        if (file_exists($views_dir . $template)) {
            include_once $views_dir . $template;
            return true;
        }
        return false;
    }

    public function zb_admin_page_index() {
        $this->load_view('index.php');
    }
  
    public function zb_admin_page_ticket() {
        $this->load_view('ticket.php');
    }
    
    public function remove_profile_fields() {
        global $pagenow;

        // apply only to user profile or user edit pages
        if ($pagenow !== 'profile.php' && $pagenow !== 'user-edit.php') {
            return;
        }

        add_action('admin_footer', function () {
            ob_start();?>
            <script>
            jQuery(document).ready( function($) {
                $('.user-admin-color-wrap').closest('tr').remove();
                // $('.user-admin-color-wrap').parents('.form-table').remove();
                $('.application-passwords').closest('div').remove();
            });
            </script>
            <?php echo ob_get_clean();
        });

    }
    


}
